/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.manager;

import org.apache.logging.log4j.util.Strings;
import org.openislands.oi.constant.AlgorithmType;
import org.openislands.oi.constant.MessageCodeEnum;
import org.openislands.oi.constant.OperatorType;
import org.openislands.oi.dao.entity.DefaultOperatorDO;
import org.openislands.oi.dao.entity.OperatorDO;
import org.openislands.oi.dao.mapper.DefaultOperatorMapper;
import org.openislands.oi.dao.mapper.OperatorMapper;
import org.openislands.oi.error.LogicException;
import org.openislands.oi.pojo.dto.OperatorDTO;
import org.openislands.oi.pojo.dto.TaskDTO;
import org.openislands.oi.util.ValueCopyUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommonOperatorManager {
    private final GeneralManager<DefaultOperatorMapper, DefaultOperatorDO> defaultOperatorMapper;

    private final GeneralManager<OperatorMapper, OperatorDO> operatorMapper;

    public CommonOperatorManager(DefaultOperatorMapper defaultOperatorMapper, OperatorMapper operatorMapper) {
        this.defaultOperatorMapper = new GeneralManager<>(defaultOperatorMapper, DefaultOperatorDO.class);
        this.operatorMapper = new GeneralManager<>(operatorMapper, OperatorDO.class);
    }

    public GeneralManager<OperatorMapper, OperatorDO> getOperatorMapper() {
        return operatorMapper;
    }

    public List<OperatorDTO> queryAnyOperator(OperatorDTO operatorDTO){
        return operatorMapper.queryAnyLikeDTO(operatorDTO);
    }

    public OperatorDTO getOperatorForTask(TaskDTO taskDTO) {
        if (Strings.isNotBlank(taskDTO.getOperatorVendor())) {
            final OperatorDTO operatorDTO = new OperatorDTO();
            operatorDTO.setType(taskDTO.getOperatorType());
            operatorDTO.setAlgorithm(taskDTO.getOperatorAlgorithm());
            operatorDTO.setVendorId(taskDTO.getOperatorVendor());
            operatorDTO.setVersion(taskDTO.getOperatorVersion());
            try {
                return operatorMapper.queryOneDTO(operatorDTO);
            }catch (Exception e){
                throw new LogicException(MessageCodeEnum.DATA_NOT_FOUND_ERROR, "the specified operator could not be found");
            }
        }
        return this.queryDefault(taskDTO.getOperatorType(), taskDTO.getOperatorAlgorithm());
    }

    public OperatorDTO queryDefault(OperatorType operatorType, AlgorithmType algorithmType) {
        List<DefaultOperatorDO> defaultOperatorDOS = defaultOperatorMapper.queryAnyList(uw -> uw
                .eq(DefaultOperatorDO::getType, operatorType)
                .eq(DefaultOperatorDO::getAlgorithm, algorithmType)
        );
        if(defaultOperatorDOS.isEmpty()){
            return null;
        }

        OperatorDTO operatorDTO = ValueCopyUtils.copyProperties(defaultOperatorDOS.get(0), new OperatorDTO());
        try {
            return operatorMapper.queryOneDTO(operatorDTO);
        } catch (Exception e) {
            throw new LogicException(MessageCodeEnum.DATA_REDUNDANCY_ERROR,  "can not found default operator for");
        }
    }

    public int mysqlInsertOrUpdate(OperatorDO operatorDO) {
        return operatorMapper.getBaseMapper().mysqlInsertOrUpdate(operatorDO);
    }

    public int mysqlInsertOrUpdate(DefaultOperatorDO defaultOperatorDO) {
        return defaultOperatorMapper.getBaseMapper().mysqlInsertOrUpdate(defaultOperatorDO);
    }
}
